package com.ivan.controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import com.ivan.domain.*;
import com.ivan.service.AccountService;
import com.ivan.service.MessageService;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller

public class AccountController {

	protected static Logger logger = Logger.getLogger("controller");
	
	@Resource(name="accountService")
	private AccountService accountService;
	@Resource(name="messageService")
	private MessageService messageService;
	
	private List<Account> accounts;
	private List<Messages> messages;
	private int accountid = -1;
		
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView getLoginPage(@RequestParam(value="error", required=false) boolean error, 
			ModelMap model) {
		System.out.println("weee");
		ModelAndView mav = new ModelAndView("blogpage");
		accounts = accountService.getAllAccounts();
	    messages = messageService.getAllMessages();
	    mav.addObject("mensahe", new Messages());
	    mav.addObject("accounts", accounts);
	    mav.addObject("messages", messages);
     	mav.addObject("msg",1);
        for(Account acc : accounts){
        	if(acc.getUsername().equals(getSessionUser())){
        		accountid = acc.getAccountid();
        	}
        }
        
        Account account = accountService.getAccount(accountid);
        mav.addObject("msg",accountid);
	    
		if (error == true) {
			model.put("error", "You have entered an invalid username or password!");
		} else {
			model.put("error", "");
		}
		
		return mav;
	}
	public static String getSessionUser() {
	    String userName = null;
	    SecurityContext securityContext = SecurityContextHolder.getContext();
	    if(securityContext != null) {
	        Authentication authentication = securityContext.getAuthentication();
	        if(authentication != null) {
	            String authenticationName = authentication.getName();
	            if(authenticationName != null) {
	                userName = authenticationName;
	            }
	        }
	    }
	    return userName;
	}
 	
 	@RequestMapping(value = "/denied", method = RequestMethod.GET)
	 	  public String getDeniedPage() {
	 	  logger.debug("Received request to show denied page");
	 	  return "denied";
 	 }
 	
 	@RequestMapping(value = "/logout", method = RequestMethod.GET)
 	public ModelAndView logout(HttpServletRequest request) {
 		accountid = -1;
 		System.out.println("hehehee "+accountid);
 		ModelAndView mav = new ModelAndView("blogpage");
 		mav.addObject("accountInput",new AccountInput());
        request.getSession().invalidate();
        messages = messageService.getAllMessages();
        mav.addObject("messages", messages);
     	accounts = accountService.getAllAccounts();
     	mav.addObject("accounts", accounts);
     	mav.addObject("msg",accountid);
     	return mav;
    }	 	
}
